package com.example.cehua;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    List<String> contents;

    public MyAdapter(List<String> contents) {
        this.contents = contents;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_, parent, false);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.content.setText(contents.get(position));
        holder.content.setOnClickListener(v -> {
            Toast.makeText(holder.itemView.getContext(), "内容" + contents.get(position), Toast.LENGTH_SHORT).show();
        });
        holder.menu2.setOnClickListener(v -> {
            Toast.makeText(holder.itemView.getContext(), "菜单" + contents.get(position), Toast.LENGTH_SHORT).show();
        });
        holder.delete.setOnClickListener(v -> {
            contents.remove(position);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return contents.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView content, delete, menu2;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);
            menu2 = itemView.findViewById(R.id.menu2);
            delete = itemView.findViewById(R.id.delete);
        }
    }
}